export enum OperationType {
  ADD_NO_JIN, // 20以内不进位加法
  SUB_NO_JIE, // 20以内不借位减法
  ADD_SUB_NO_FIX, // 20以内不进、借位加减法
  ADD_TEN, // 凑十法
}
export function twentyGenerate(
  maxNum: number,
  type: OperationType,
  lines: number = 15,
): string[][] {
  const result: string[][] = [];
  const totalGroups = lines;
  const problemsPerGroup = 4;

  // 生成所有需要的题目
  const problems: string[] = [];
  // 这是个每次加2的种子为了第三种生成的公式中有加法有减法
  let tempSeed = 0;
  while (problems.length < totalGroups * problemsPerGroup) {
    let temps = [];
    switch (type) {
      case OperationType.ADD_NO_JIN: {
        temps = generateValidProblem(maxNum);
        const tempAddExpression = `${temps[0]} + ${temps[1]} =`;
        // 保证数组中不重复
        if (!problems.includes(tempAddExpression))
          problems.push(tempAddExpression);
        break;
      }
      case OperationType.ADD_SUB_NO_FIX: {
        temps =
          tempSeed % 2 === 0
            ? generateValidProblem(maxNum)
            : generateSubNumber(maxNum);
        const tempFixExpression =
          tempSeed % 2 === 0
            ? `${temps[0]} + ${temps[1]} =`
            : `${temps[0]} - ${temps[1]} =`;
        // 保证数组中不重复
        if (!problems.includes(tempFixExpression))
          problems.push(tempFixExpression);
        tempSeed += 1;
        break;
      }
      case OperationType.ADD_TEN: {
        temps = generateGt(maxNum);
        const tempAddExpression = `${temps[0]} + ${temps[1]} =`;
        // 保证数组中不重复
        if (!problems.includes(tempAddExpression))
          problems.push(tempAddExpression);
        break;
      }
      case OperationType.SUB_NO_JIE: {
        temps = generateSubNumber(maxNum);
        const tempSubExpression = `${temps[0]} - ${temps[1]} =`;
        // 保证数组中不重复
        if (!problems.includes(tempSubExpression))
          problems.push(tempSubExpression);
        break;
      }
    }
  }
  // 将题目分组
  for (let i = 0; i < problems.length; i += problemsPerGroup) {
    result.push(problems.slice(i, i + problemsPerGroup));
  }
  return result;
}
// 加法不进位算出有效的两个数字
function generateValidProblem(maxNum: number): [number, number] {
  let oneNum: number, twoNum: number;
  do {
    oneNum = Number.parseInt(String(Math.random() * maxNum));
    twoNum = Number.parseInt(String(Math.random() * maxNum));
  } while (
    (oneNum % 10) + (twoNum % 10) > 9 ||
    (Math.floor(oneNum / 10) > 0 && Math.floor(twoNum / 10) > 0)
  ); // 检查个位是否进位
  return [oneNum, twoNum];
}
// 减法不借位算出两个有效数字
function generateSubNumber(maxNum: number): [number, number] {
  let oneNum: number, twoNum: number;
  do {
    oneNum = Number.parseInt(String(Math.random() * maxNum));
    twoNum = Number.parseInt(String(Math.random() * maxNum));
  } while (
    oneNum % 10 < twoNum % 10 ||
    Math.floor(oneNum / 10) < Math.floor(twoNum / 10) ||
    (Math.floor(oneNum / 10) > 0 && Math.floor(twoNum / 10) > 0)
  );
  return [oneNum, twoNum];
}
// 凑十法
function generateGt(maxNum: number): [number, number] {
  let oneNum: number, twoNum: number;
  do {
    oneNum = Number.parseInt(String(Math.random() * maxNum));
    twoNum = Number.parseInt(String(Math.random() * maxNum));
  } while (oneNum + twoNum <= 10);
  return [oneNum, twoNum];
}
